package com.xtaller.easy.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.easy.admin.base.TApi;
import com.xtaller.easy.admin.config.R;
import com.xtaller.easy.admin.model.SysLabel;
import com.xtaller.easy.admin.service.impl.SysLabelService;
import com.xtaller.easy.admin.specification.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2018/4/16
 */
@Api(tags = "数据标签api")
@RestController
@RequestMapping("/v1/sys")
@CrossOrigin   //跨域服务注解
public class LabelApi extends TApi{
    @Autowired
    private SysLabelService labelService;

    private Map<String,String> LabelModuleVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("name","标签模块名称不能为空");
        verify.put("code","标签模块编码不能为空");
        return verify;
    }
    private Map<String,String> LabelGroupVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("name","标签分组名称不能为空");
        verify.put("moduleId","标签模块Id不能为空");
        verify.put("code","标签分组编码不能为空");
        return verify;
    }
    private Map<String,String> LabelVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("moduleId","标签模块Id不能为空");
        verify.put("parentId","标签父级Id不能为空");
        verify.put("name","标签名称不能为空");
        verify.put("code","标签编码不能为空");
        return verify;
    }
    private Map<String,String> LabelVerify2(){
        Map<String,String> verify = new HashMap<>();
        verify.put("name","标签名称不能为空");
        verify.put("code","标签编码不能为空");
        return verify;
    }
    /***********************标签模块************************/
    @GetMapping("/label-module")
    @ApiOperation(value = "读取标签模块")
    public Object getModule(){
        return R.ok(labelService.getModule());
    }

    @PostMapping("/label-module")
    @ApiOperation(value = "创建标签模块")
    public Object createModule(@RequestBody LabelModuleCreate object){
        JSONObject check = V.checkEmpty(LabelModuleVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setCreator(getUserIdByToken());
        // 模块唯一性判断
        if(labelService.existModule(label, true))
            return R.error("模块名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, true))
            return R.error("模块编码已经存在请更换");
        label = labelService.createSysLabel(label);
        if(label == null)
            return R.error("创建模块失败,请稍后再试");
        return R.ok("创建模块成功", formatObject(label));
    }

    @PutMapping("/label-module")
    @ApiOperation(value = "修改标签模块")
    public Object updateModule(@RequestBody LabelModuleUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(labelService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(LabelModuleVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if(labelService.existModule(label, false))
            return R.error("模块名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, false))
            return R.error("模块编码已经存在请更换");
        label = labelService.updateSysLabel(label);
        if(label == null)
            return R.error("修改模块失败,请稍后再试");
        return R.ok("修改模块成功", formatObject(label));
    }

    @DeleteMapping("/label-module/{id}")
    @ApiOperation(value = "删除标签模块")
    public Object deleteModule(@PathVariable("id") String id){
        if(labelService.selectById(id) == null)
            return R.error("数据Id异常");
        List<SysLabel> list = labelService.getByModuleIdOrParentId(id);
        if(list.size() > 0)
            return R.error("该模块下有子集数据，禁止删除！");
        if(labelService.deleteSysLabel(id, getUserIdByToken()))
            return R.ok("标签模块删除成功");
        return R.error("删除失败请稍后再试");
    }
    /************************标签分组*************************/
    @GetMapping("/label-group/{moduleId}")
    @ApiOperation(value = "读取标签分组")
    public Object getGroup(@PathVariable("moduleId") Long moduleId){
        return R.ok(labelService.getByModuleId(moduleId));
    }

    @PostMapping("/label-group")
    @ApiOperation(value = "创建标签分组")
    public Object createGroup(@RequestBody LabelGroupCreate object){
        JSONObject check = V.checkEmpty(LabelGroupVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setCreator(getUserIdByToken());
        label.setParentId(object.getModuleId());
        // 模块唯一性判断
        if(labelService.existGroup(label, true))
            return R.error("标签分组名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, true))
            return R.error("标签分组编码已经存在请更换");
        label = labelService.createSysLabel(label);
        if(label == null)
            return R.error("创建标签分组失败,请稍后再试");
        return R.ok("创建标签分组成功", formatObject(label));
    }

    @PutMapping("/label-group")
    @ApiOperation(value = "修改标签模块")
    public Object updateGroup(@RequestBody LabelGroupUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(labelService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(LabelGroupVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if(labelService.existGroup(label, false))
            return R.error("标签分组名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, false))
            return R.error("标签分组编码已经存在请更换");
        label = labelService.updateSysLabel(label);
        if(label == null)
            return R.error("修改标签分组失败,请稍后再试");
        return R.ok("修改标签分组成功", formatObject(label));
    }

    @DeleteMapping("/label-group/{id}")
    @ApiOperation(value = "删除标签分组")
    public Object deleteGroup(@PathVariable("id") String id){
        if(labelService.selectById(id) == null)
            return R.error("数据Id异常");
        List<SysLabel> list = labelService.getByModuleIdOrParentId(id);
        if(list.size() > 0)
            return R.error("该分组下有子集数据，禁止删除！");
        if(labelService.deleteSysLabel(id, getUserIdByToken()))
            return R.ok("删除标签分组删除成功");
        return R.error("删除失败请稍后再试");
    }
    /**************************标签**************************/
    @GetMapping("/label/{parentId}")
    @ApiOperation(value = "读取标签")
    public Object getLabelByParentId(@PathVariable("parentId") String parentId){
        return R.ok(labelService.getByParentId(parentId));
    }

    @PostMapping("/label")
    @ApiOperation(value = "创建标签")
    public Object createLabel(@RequestBody LabelCreate object){
        JSONObject check = V.checkEmpty(LabelVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setCreator(getUserIdByToken());
        // 模块唯一性判断
        if(labelService.existName(label, true))
            return R.error("标签名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, true))
            return R.error("标签编码已经存在请更换");
        label = labelService.createSysLabel(label);
        if(label == null)
            return R.error("创建标签失败,请稍后再试");
        return R.ok("创建标签成功", formatObject(label));
    }

    @PutMapping("/label")
    @ApiOperation(value = "修改标签")
    public Object updateLabel(@RequestBody LabelUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(labelService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(LabelVerify2(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysLabel label = J.o2m(object, SysLabel.class);
        label.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if(labelService.existName(label, false))
            return R.error("标签名称已经存在请更换");
        // 编码唯一性判断
        if(labelService.existCode(label, false))
            return R.error("标签编码已经存在请更换");
        label = labelService.updateSysLabel(label);
        if(label == null)
            return R.error("修改标签失败,请稍后再试");
        return R.ok("修改标签成功", formatObject(label));
    }

    @DeleteMapping("/label/{id}")
    @ApiOperation(value = "删除标签")
    public Object deleteLabel(@PathVariable("id") String id){
        if(labelService.selectById(id) == null)
            return R.error("数据Id异常");
        if(labelService.deleteSysLabel(id, getUserIdByToken()))
            return R.ok("标签删除成功");
        return R.error("删除失败请稍后再试");
    }

    @GetMapping("/label-tree")
    @ApiOperation(value = "读取标签树数据")
    public Object getLabelTree(){
        return R.ok(labelService.getTreeLabel());
    }

    @GetMapping("/label-query/{code}")
    @ApiOperation(value = "通过标签code读取标签树数据")
    public Object getLabelByCode(@ApiParam("标签模块.标签分组 例如：user@info 、 system") @PathVariable("code") String code){
        return R.ok(labelService.getLabelByCode(code));
    }
}
